/**
 * 零部件信息Info*E Task类
 * 
 * @author 魏文杰 
 * @Version 1.0
 * @Date 2013-10-08
 */

package ext.generic.erp.task;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import wt.part.WTPart;

import ext.generic.erp.attributes.AttributeUtil;
import ext.generic.erp.config.ERPPropertiesUtil;
import ext.generic.erp.util.IEXMLUtil;
import ext.generic.erp.util.IntegrationConstant;
import ext.generic.erp.util.PDMIntegrationLogUtil;
import ext.generic.erp.common.CommonUtil;

import com.infoengine.SAK.Task;
import com.infoengine.util.IEException;
import com.infoengine.object.factory.Group;

public class PartIETask {
	
	private static String clazz = PartIETask.class.getName() ;
	
	private static boolean verbose = PDMIntegrationLogUtil.verbose ;
	
	private static String ieXMLFileQuery = null ;
	private static String ieXMLFileCreate = null;
	private static String ieXMLFileUpdate = null ;
	private static String ieXMLFileDelete = null ;
	
	private static IEXMLUtil ieXMLUtil = new IEXMLUtil();
	
	static {
		ieXMLFileQuery = (String) ERPPropertiesUtil.getInstance().readProperties().get(IntegrationConstant.PART_QUERY_IE_XML_FILE);
		ieXMLFileCreate = (String) ERPPropertiesUtil.getInstance().readProperties().get(IntegrationConstant.PART_CREATE_IE_XML_FILE);
		ieXMLFileUpdate = (String) ERPPropertiesUtil.getInstance().readProperties().get(IntegrationConstant.PART_UPDATE_IE_XML_FILE);
		ieXMLFileDelete = (String) ERPPropertiesUtil.getInstance().readProperties().get(IntegrationConstant.PART_DELETE_IE_XML_FILE);
		
		initialCheck();
	}

	protected static void initialCheck() {
		if(ieXMLFileQuery != null){
			ieXMLFileQuery = ieXMLFileQuery.trim() ;
			
			PDMIntegrationLogUtil.printLog(clazz, "Query Config File : " + ieXMLFileQuery , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}else{
			PDMIntegrationLogUtil.printLog(clazz, "Query Config File : NULL" , IntegrationConstant.LOG_LEVEL_ERROR ) ;
		}
		
		if(ieXMLFileCreate != null){
			ieXMLFileCreate = ieXMLFileCreate.trim() ;
			
			PDMIntegrationLogUtil.printLog(clazz, "Create Config File : " + ieXMLFileCreate , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}else{
			PDMIntegrationLogUtil.printLog(clazz, "Create Config File : NULL" , IntegrationConstant.LOG_LEVEL_ERROR ) ;
		}
		
		if(ieXMLFileUpdate != null){
			ieXMLFileUpdate = ieXMLFileUpdate.trim() ;
			
			PDMIntegrationLogUtil.printLog(clazz, "Update Config File : " + ieXMLFileUpdate , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}else{
			PDMIntegrationLogUtil.printLog(clazz, "Update Config File : NULL" , IntegrationConstant.LOG_LEVEL_ERROR ) ;
		}
		
		if(ieXMLFileDelete != null){
			ieXMLFileDelete = ieXMLFileDelete.trim() ;
			
			PDMIntegrationLogUtil.printLog(clazz, "Delete Config File : " + ieXMLFileDelete , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}else{
			PDMIntegrationLogUtil.printLog(clazz, "Delete Config File : NULL" + ieXMLFileDelete , IntegrationConstant.LOG_LEVEL_ERROR ) ;
		}
	}
	
	/**
	 * 从中间表查询零部件
	 * 
	 * @param part
	 * @return Group
	 */
	public static Group query(WTPart part){
		PDMIntegrationLogUtil.printLog(clazz, "Entering In query(WTPart part)......") ;
		
		Group group = null ;
		
		//创建任务
		Task task = new Task( ieXMLFileQuery );
		
		IETaskCommonUtil.addWhere(part, task) ;

		//执行任务
		try {
			PDMIntegrationLogUtil.printLog(clazz, "准备执行零部件信息查询Task......") ;
			
			task.invoke();
			
			PDMIntegrationLogUtil.printLog(clazz, "零部件信息查询Task执行完成......") ;
		} catch (IEException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IEException..." , IntegrationConstant.LOG_LEVEL_ERROR) ;
			PDMIntegrationLogUtil.printLog(clazz, e.toString() , IntegrationConstant.LOG_LEVEL_ERROR) ;
			
			e.printStackTrace();
		} catch (IOException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IOException..." , IntegrationConstant.LOG_LEVEL_ERROR) ;
			PDMIntegrationLogUtil.printLog(clazz, e.getMessage() , IntegrationConstant.LOG_LEVEL_ERROR) ;
			
			e.printStackTrace();
		}
		
		//获取返回值
		group =  task.getGroup("QueryObjects");
		
		PDMIntegrationLogUtil.printLog(clazz, "Existed Out query(WTPart part)......") ;
		
		return group ;
	}
	
	/**
	 * 判断中间表是否有WTPart
	 * 
	 * @param part
	 * @return boolean
	 */
	public static boolean hasObject(WTPart part){
		PDMIntegrationLogUtil.printLog(clazz, "Entering In hasObject(WTPart part)......") ;
		
		Group group =   query( part ) ;
		
		boolean hasPart = IETaskCommonUtil.hasObject( group ) ;
		
		PDMIntegrationLogUtil.printLog(clazz, "hasPart = " + hasPart ) ;
		
		PDMIntegrationLogUtil.printLog(clazz, "Existed Out hasObject(WTPart part)......") ;
		
		return hasPart ;
	}
	
	/**
	 * 在中间表中，添加零部件信息
	 * 
	 * @param part
	 */
	public static void add(WTPart part){
		add( part , "" ) ;
	}
	
	/**
	 * 在中间表中，添加零部件信息
	 * 
	 * @param part
	 * @param releaseTime
	 */
	public static void add(WTPart part , String releaseTime){
		PDMIntegrationLogUtil.printLog(clazz, "Entering In add(WTPart part , String releaseTime)......") ;
		
		boolean hasPart = hasObject( part ) ;
		
		PDMIntegrationLogUtil.printLog(clazz, "hasPart = " + hasPart ) ;
		
		if( hasPart ){
			PDMIntegrationLogUtil.printLog(clazz, "执行更新零部件信息操作") ;
			
			update(part , releaseTime ) ;
		}else{
			PDMIntegrationLogUtil.printLog(clazz, "执行创建零部件信息操作") ;
			
			create(part , releaseTime ) ;
		}
		
		PDMIntegrationLogUtil.printLog(clazz, "Existed Out add(WTPart part , String releaseTime)......") ;
	}
	
	/**
	 * 从中间表中删除零部件的信息
	 * 
	 * @param part
	 */
	public static void delete(WTPart part){
		PDMIntegrationLogUtil.printLog(clazz, "Entering In delete(WTPart part)......") ;
		
		if( part == null ){
			PDMIntegrationLogUtil.printLog(clazz, "delete Method, Part is NULL", IntegrationConstant.LOG_LEVEL_WARN);
			return ;
		}
		
		//创建任务
		Task task = new Task(ieXMLFileDelete);
		
		IETaskCommonUtil.addWhereForDelete(part, task);
		
		try {
			PDMIntegrationLogUtil.printLog(clazz, "执行零部件信息删除操作") ;
			task.invoke();
			PDMIntegrationLogUtil.printLog(clazz, "删除零部件信息任务结束") ;
		} catch (IEException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IEException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.toString() , IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		} catch (IOException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IOException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.getMessage(), IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		}finally{
			PDMIntegrationLogUtil.printLog(clazz, "Existed Out delete(WTPart part)......") ;
		}
	}
	
	/**
	 * 在中间表中创建记录
	 * 
	 * @param part
	 * @param releaseTime
	 */
	protected static void create(WTPart part , String releaseTime) {
		PDMIntegrationLogUtil.printLog(clazz, "Entering In create(WTPart part , String releaseTime)......") ;
		
		if( part == null ){
			PDMIntegrationLogUtil.printLog(clazz, "create Method, Part is NULL", IntegrationConstant.LOG_LEVEL_WARN);
			return ;
		}
		
		//从IE Task的XML文件中解析属性映射关系
		Map<String,String> fieldMap = ieXMLUtil.parserXml(IETaskCommonUtil.TASK_FILE_DIRECOTRY +  ieXMLFileCreate );
		
		if(verbose){
			PDMIntegrationLogUtil.printLog(clazz, CommonUtil.getMapStringValue( fieldMap ) , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}
		
		Task task = new Task( ieXMLFileCreate );

		Iterator<String> ite = fieldMap.keySet().iterator() ;
		
		//根据属性映射关系，发布属性值
		while(ite.hasNext()){
			String dbField = ite.next();
			String attributeId = fieldMap.get( dbField );
			
			String attributeValue = AttributeUtil.getAttribute( part , attributeId ) ;
			
			task.addParam( attributeId , attributeValue );
		}
		
		IETaskCommonUtil.addPartProcessStatus(task ,releaseTime);
		
		IETaskCommonUtil.updatePDMWritebackStatus(task);
		
		if(verbose){			
			PDMIntegrationLogUtil.printLog(clazz, IETaskCommonUtil.getTaskParams(task) );
		}
		
		try {
			PDMIntegrationLogUtil.printLog(clazz, "执行零部件信息创建任务") ;
			task.invoke();
			PDMIntegrationLogUtil.printLog(clazz, "创建零部件信息任务结束") ;
		} catch (IEException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IEException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.toString() , IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		} catch (IOException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IOException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.getMessage(), IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		}finally{
			PDMIntegrationLogUtil.printLog(clazz, "Existed Out create(WTPart part , String releaseTime)......") ;
		}
	}
	
	
	/**
	 * 在中间表中更新记录
	 * 
	 * @param part
	 * @param releaseTime
	 */
	protected static void update(WTPart part , String releaseTime){
		PDMIntegrationLogUtil.printLog(clazz, "Entering In update(WTPart part , String releaseTime)......") ;
		
		if( part == null ){
			PDMIntegrationLogUtil.printLog(clazz, "update Method, Part is NULL", IntegrationConstant.LOG_LEVEL_WARN);
			return ;
		}
		
		//从IE Task的XML文件中解析属性映射关系
		Map<String,String> fieldMap = ieXMLUtil.parserXml( IETaskCommonUtil.TASK_FILE_DIRECOTRY + ieXMLFileUpdate );
		
		if(verbose){
			PDMIntegrationLogUtil.printLog(clazz, CommonUtil.getMapStringValue( fieldMap ) , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}
		
		//创建任务
		Task task = new Task(ieXMLFileUpdate);
		
		Iterator<String> ite = fieldMap.keySet().iterator() ;
		
		//根据属性映射关系，发布属性值
		while(ite.hasNext()){
			String dbField = ite.next();
			String attributeId = fieldMap.get( dbField );
			
			String attributeValue = AttributeUtil.getAttribute( part , attributeId ) ;
			
			task.addParam( attributeId , attributeValue );
		}
		
		IETaskCommonUtil.addPartProcessStatus(task , releaseTime);
		
		IETaskCommonUtil.updatePDMWritebackStatus(task);
		
		IETaskCommonUtil.addWhere(part, task) ;
		
		if(verbose){			
			PDMIntegrationLogUtil.printLog(clazz, IETaskCommonUtil.getTaskParams(task) );
		}
		
		try {
			PDMIntegrationLogUtil.printLog(clazz, "执行零部件信息更新任务") ;
			task.invoke();
			PDMIntegrationLogUtil.printLog(clazz, "更新零部件信息任务结束") ;
		} catch (IEException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IEException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.toString() , IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		} catch (IOException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IOException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.getMessage(), IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		}finally{
			PDMIntegrationLogUtil.printLog(clazz, "Existed Out update(WTPart part , String releaseTime)......") ;
		}
	}
}
